package com.franzliszt.OrderOnline.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.LinearLayoutManager;


import com.franzliszt.OrderOnline.tool.ViewTool;
import com.franzliszt.hospitalnumberingsystem.R;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Title extends Fragment {
    private String[] medicineContent = {"[药圣堂]复方感冒灵","[感康]复方氨酚烷胺胶囊","[芬必得]布洛芬缓释胶囊","[太极]藿香正气水口服液","[999]感冒灵颗粒","[以岭]连花清瘟胶囊","[三金]西瓜霜润喉片","[白云山]板蓝根颗粒","[药圣堂]感冒清热胶囊","[勃然]风热感冒颗粒"};
    private String[] medicineSpec = {"13袋/盒","20粒/盒","24粒/盒","12片/盒","10支/盒","9袋/盒","36粒/盒","36片/盒","30袋/包","20粒/盒","10袋/盒"};
    private double[] medicinePrice = {22.90,16.62,29.57,16.00,19.84,7.30,21.90,6.50,16.95,19.90,22.00};
    private int[] medicineImg = {R.drawable.fufang,R.drawable.cold,R.drawable.buluofen,R.drawable.huoxiang,R.drawable.cold999,R.drawable.lianhua,R.drawable.xiguashuang,R.drawable.banlangen,R.drawable.cold_qingre,R.drawable.fengre,};
    private String[] listTitleName = {"感冒用药","家庭常备","幸福生活","妇科用药","男科用药","滋补养生","清热解毒","儿童用药","皮肤用药","抗菌消炎","其他用药"};
    private boolean flag;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate( R.layout.titleview,container,false );
        RecyclerView titleView = view.findViewById( R.id.titleRecycler );
        LinearLayoutManager manager = new LinearLayoutManager( getActivity() );
        titleView.setLayoutManager( manager );
        Adapter adapter = new Adapter( getData() );
        titleView.setAdapter( adapter );
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated( savedInstanceState );
        if (getActivity().findViewById(R.id.content_fragment) != null)
            flag = true;
        else
            flag = false;
    }
    private List<String> medicineType(){
        List<String> stringList = new ArrayList<>(  );
        stringList = Arrays.asList( listTitleName );
        return stringList;
    }
    private List<ViewTool> getData(){
        List<ViewTool> listTool = new ArrayList<>(  );
        for (int i = 0; i < 10; i++) {
                ViewTool tool = new ViewTool( medicineImg[i],medicineContent[i],medicineSpec[i],medicinePrice[i],R.drawable.icon_add,R.drawable.icon_sub,1,listTitleName[i] );
            listTool.add( tool );
        }
        return listTool;
    }
    class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder>{
        private List<ViewTool> toolsList;
        public Adapter(List<ViewTool> toolsList){
            this.toolsList = toolsList;
        }
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, int viewType) {
            View view = LayoutInflater.from( parent.getContext() ).inflate( R.layout.title_item,parent,false );
            final ViewHolder viewHolder = new ViewHolder( view );
            view.setOnClickListener( new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ViewTool tool = toolsList.get( viewHolder.getAdapterPosition() );
                    if (flag) {
                        Content content = (Content) getFragmentManager().findFragmentById(R.id.content_fragment);
                        content.Refresh(tool.getGoodsIMG(),tool.getContent(),tool.getSpecs(),tool.getPrice(),tool.getAddIMG(),tool.getSubIMG(),tool.getNumber(),tool.getType());
                    } else {
                        //NewsContent.ActionStart(view.getContext(), news.getTitle(), news.getContent());
                    }
                }
            } );
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            ViewTool tool = toolsList.get( position );
            holder.TitleName.setText( tool.getType() );

        }

        @Override
        public int getItemCount() {
            return toolsList.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder{
            private TextView TitleName;

            public ViewHolder(@NonNull View itemView) {
                super( itemView );
                TitleName = itemView.findViewById( R.id.title_name );
            }
        }
    }
}
